package dao

import (
	"gorm.io/gorm"
	"gowork/microSignalSMSSending/model"
	"time"
)

type DistributionRecordsImf struct {

}

type DistributionWeChat struct {
	Phone string
	WeChatCardUrl string
	WeChatNo string
	CustomerId int64
	CustomerName string
	Account string
}

func (distributionRecordsImf *DistributionRecordsImf) GetAllocateTimeByCustomerId(customerId int64, tx *gorm.DB)(time.Time , error)  {
	var  distributionRecords model.DistributionRecords
	err := tx.Where("customer_id = ?", customerId).First(&distributionRecords).Error
	if err != nil {
		return time.Time{}, err
	}

	return distributionRecords.AllocateTime, nil
}

func (distributionRecordsImf *DistributionRecordsImf) GetSendingWeChatRecord(tx *gorm.DB)([]*DistributionWeChat, error)  {
	var dwcList []*DistributionWeChat
	err := tx.Raw("select c.phone, awci.we_chat_card_url, awci.we_chat_no, c.id, c.name, dr.consultant_account from distribution_records dr inner join adviser a on a.account = dr.consultant_account inner join adviser_we_chat_info awci on dr.consultant_account = awci.account inner join customer c on c.id = dr.customer_id where dr.customer_id not in (select customer_id from sending_mode_record) and dr.customer_id not in (select customer_id from sms_record where sending_type = 2) and a.group_leader = false and dr.allocate_time < ?", time.Now().AddDate(0 ,0 ,-3).Format("2006-01-02 15:04:05")).Scan(&dwcList).Error
	if err != nil {
		return nil, err
	}

	return dwcList, nil
}


